<?php
/**
 * Object Relational Persistence Mapping Class for table: ticket_medicine
 *
 * @package core.ordo
 * @author LHV
 * @version $Id$
 * @copyright 2009 LHV Company
 *
 */
$loader->requireOnce('ordo/Ticket.class.php');
class TicketMedicine extends ORDataObject {

	/**#@+
	 * Fields of table: ticket_medicine mapped to class members
	 */
	var $ticket_medicine_id	   = '';
	var $ticket_id		       = '';
	var $medicine_id           = '';
	var $quantity_num          = '';
	var $quantity_title        = '';
	var $unit_price            = '';
	var $original_price        = '';
	var $insurance_price       = '';
	var $patient_price         = '';
	var $issue_date            = '';
	var $issue_person          = '';
	var $doctor_id             = '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'ticket_medicine';

	/**
	 * Primary Key
	 */
	var $_key = 'ticket_medicine_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'TicketMedicine';

	/**
	 * Handle instantiation
	 */
	function TicketMedicine() {
		parent::ORDataObject();
	}

	/**#@+
     * Getters
     */
	public function getTicketId(){
		return $this->ticket_id;
	}

	public function getMedicineId(){
		return $this->medicine_id;
	}

	public function getQuantityNumber(){
		return $this->quantity_num;
	}

	public function getQuantityTitle(){
		return $this->quantity_title;
	}

    public function getUnitPrice(){
        return $this->unit_price;
    }

	public function getOriginalPrice(){
		return $this->original_price;
	}

	public function getInsurancePrice(){
		return $this->insurance_price;
	}

	public function getIssueDate(){
		return L10nHelper::renderDateTime($this->issue_date);
	}

	public function getIssuePerson(){
		return $this->issue_person;
	}

	public function getDoctorId(){
		return $this->doctor_id;
	}
	/**#@-*/

	/**
	 * Set data to the object.
	 *
	 * @param int $ticketId Ticket Id
	 * @param int $medId Medicine Id
	 * @param int $quanNum Medicine quantity number
	 * @param string $quanTitle Medicine quantity title
	 * @param mixed $unitPrice Medicine unit price
	 * @param mixed $orgPrice Total price of the medicine
	 * @param mixed $insPrice Total payment which Insurance Agent must paid for the medicine
	 * @param mixed $patPrice Total payment which patient must paid for the medicine
	 * @param mixed $issueDate Date issued medicine
	 * @param mixed $issuePerson Peson who issued medicine
	 * @param mixed $doctorId Treatment doctor
	 */
	public function setData($ticketId, $medId, $quanNum, $quanTitle, $unitPrice, $orgPrice,
	       $insPrice, $patPrice, $issueDate, $issuePerson, $doctorId){
	   $data   =   array(
            'ticket_id'         =>    $ticketId,
		    'medicine_id'       =>    $medId,
		    'quantity_num'      =>    $quanNum,
		    'quantity_title'    =>    $quanTitle,
		    'unit_price'        =>    $unitPrice,
		    'original_price'    =>    $orgPrice,
		    'insurance_price'   =>    $insPrice,
		    'patient_price'     =>    $patPrice,
		    'issue_date'        =>    $issueDate,
		    'issue_person'      =>    $issuePerson,
		    'doctor_id'         =>    $doctorId
	   );
	   $this->populateArray($data);
	}

	/**
	 * Get all TicketItem of a given ticket which issued in a provided range of date.
	 *
	 * @param int $ticketId Ticket Id
	 * @param string $dateFrom Start date
	 * @param string $dateTo End date
	 * @param bool $filter Filter result by date or not
	 * @return array Result info array
	 */
	public function getTicketMedicine($ticketId, $dateFrom, $dateTo, $filter){	    
		
        $dateFormat =   L10nHelper::getSqlDateFormat();
        $sql    =   array(
            'cols'  =>  "DATE_FORMAT(t.issue_date, '$dateFormat') as issue_date, t.medicine_id, m.name as medicine_name,
                t.unit_price, t.original_price, t.insurance_price, t.patient_price, t.quantity_num, t.quantity_title,
                t.issue_person, p1.name as issue_person_name, t.doctor_id, p2.name as doctor",
            'from'  =>  "$this->_table t
                LEFT JOIN medicine m ON t.medicine_id=m.medicine_id                
                LEFT JOIN person p1 ON t.issue_person=p1.person_id
                LEFT JOIN person p2 ON t.doctor_id=p2.person_id",
            'where' =>  "t.ticket_id=$ticketId",
            'orderby'  => "issue_date, medicine_name");
        if($filter){
        	$sql['where']  .=   " AND DATE(t.issue_date) >= '$dateFrom' AND DATE(t.issue_date) <= '$dateTo'";
        }

        $ds     =   & new Datasource_sql();
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
	}

	/**
	 * Delete all TicketMedicine item of a given ticket.
	 *
	 * @param int $ticketId Ticket Id
	 */
	public function deleteItem($ticketId, $issueDate){
        $sql    =   "DELETE FROM $this->_table WHERE ticket_id='$ticketId' AND issue_date='$issueDate'";
        $this->_execute($sql);
	}

	/**
	 * Get information on medicine a patient has used in a resident examination at hospital.
	 *
	 * @param int $patientId Patient Id
	 * @return array Result array
	 */
	public function getMedicineByPatient($patientId) {
	    
        $dateFormat =   L10nHelper::getSqlDateFormat();
        $status     =   Ticket::OPEN_STATUS;
        $sql    =   array(
            'cols'  =>  "DATE_FORMAT(t.issue_date, '$dateFormat') as issue_date, t.medicine_id, m.name as medicine_name,
                t.unit_price, t.original_price, t.insurance_price, t.patient_price, t.quantity_num, t.quantity_title,
                t.issue_person, p1.name as issue_person_name, t.doctor_id, p2.name as doctor",
            'from'  =>  "$this->_table t
                INNER JOIN ticket tk ON t.ticket_id=tk.ticket_id
                LEFT JOIN medicine m ON t.medicine_id=m.medicine_id        
                LEFT JOIN person p1 ON t.issue_person=p1.person_id
                LEFT JOIN person p2 ON t.doctor_id=p2.person_id",
            'where' =>  "tk.patient_id=$patientId AND tk.status='$status'",
            'orderby'  => "t.issue_date"
        );

        $ds     =   & new Datasource_sql();
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
	}
}
?>
